/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biness.product.service.impl;

import me.zhengjie.modules.biness.product.domain.THfDiscountDetail;
import me.zhengjie.modules.biness.product.repository.THfDiscountDetailRepository;
import me.zhengjie.modules.biness.product.service.THfDiscountDetailService;
import me.zhengjie.modules.biness.product.service.dto.THfDiscountDetailDto;
import me.zhengjie.modules.biness.product.service.dto.THfDiscountDetailQueryCriteria;
import me.zhengjie.modules.biness.product.service.mapstruct.THfDiscountDetailMapper;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author yanzz
* @date 2021-02-19
**/
@Service
@RequiredArgsConstructor
public class THfDiscountDetailServiceImpl implements THfDiscountDetailService {

    private final THfDiscountDetailRepository tHfDiscountDetailRepository;
    private final THfDiscountDetailMapper tHfDiscountDetailMapper;

    @Override
    public Map<String,Object> queryAll(THfDiscountDetailQueryCriteria criteria, Pageable pageable){
        Page<THfDiscountDetail> page = tHfDiscountDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(tHfDiscountDetailMapper::toDto));
    }

    @Override
    public List<THfDiscountDetailDto> queryAll(THfDiscountDetailQueryCriteria criteria){
        return tHfDiscountDetailMapper.toDto(tHfDiscountDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public THfDiscountDetailDto findById(Integer id) {
        THfDiscountDetail tHfDiscountDetail = tHfDiscountDetailRepository.findById(id).orElseGet(THfDiscountDetail::new);
        ValidationUtil.isNull(tHfDiscountDetail.getId(),"THfDiscountDetail","id",id);
        return tHfDiscountDetailMapper.toDto(tHfDiscountDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public THfDiscountDetailDto create(THfDiscountDetail resources) {
        return tHfDiscountDetailMapper.toDto(tHfDiscountDetailRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(THfDiscountDetail resources) {
        THfDiscountDetail tHfDiscountDetail = tHfDiscountDetailRepository.findById(resources.getId()).orElseGet(THfDiscountDetail::new);
        ValidationUtil.isNull( tHfDiscountDetail.getId(),"THfDiscountDetail","id",resources.getId());
        tHfDiscountDetail.copy(resources);
        tHfDiscountDetailRepository.save(tHfDiscountDetail);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            tHfDiscountDetailRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<THfDiscountDetailDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (THfDiscountDetailDto tHfDiscountDetail : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("档次说明", tHfDiscountDetail.getGrade());
            map.put("充值范围", tHfDiscountDetail.getRecharge());
            map.put("折扣", tHfDiscountDetail.getDiscount());
            map.put("折扣明细", tHfDiscountDetail.getDiscountStr());
            map.put("折扣版本", tHfDiscountDetail.getDiscountVersion());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}